package org.dromara.school.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.school.domain.vo.DisciplineVo;
import org.dromara.school.listener.DisciplineImportExcelListener;
import org.dromara.school.listener.TeacherImportExcelListener;
import org.dromara.school.service.ICampusService;
import org.dromara.school.service.IDisciplineService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.school.domain.vo.TeacherVo;
import org.dromara.school.domain.bo.TeacherBo;
import org.dromara.school.service.ITeacherService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师管理
 *
 * @author fliay
 * @date 2025-05-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/school/teacher")
public class TeacherController extends BaseController {

    private final ITeacherService teacherService;

    private final ICampusService campusService;

    private final IDisciplineService disciplineService;


    /**
     * 查询教师管理列表
     */
    @SaCheckPermission("school:teacher:list")
    @GetMapping("/list")
    public TableDataInfo<TeacherVo> list(TeacherBo bo, PageQuery pageQuery) {
        return teacherService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出教师管理列表
     */
    @SaCheckPermission("school:teacher:export")
    @Log(title = "教师管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TeacherBo bo, HttpServletResponse response) {
        List<TeacherVo> list = teacherService.queryList(bo);
        ExcelUtil.exportExcel(list, "教师管理", TeacherVo.class, response);
    }

    /**
     * 获取教师管理详细信息
     *
     * @param teacherId 主键
     */
    @SaCheckPermission("school:teacher:query")
    @GetMapping("/{teacherId}")
    public R<TeacherVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long teacherId) {

        TeacherVo teacherVo = teacherService.queryById(teacherId);

        return R.ok(teacherService.queryById(teacherId));
    }

    /**
     * 新增教师管理
     */
    @SaCheckPermission("school:teacher:add")
    @Log(title = "教师管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TeacherBo bo) {
        return toAjax(teacherService.insertByBo(bo));
    }

    /**
     * 修改教师管理
     */
    @SaCheckPermission("school:teacher:edit")
    @Log(title = "教师管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TeacherBo bo) {
        return toAjax(teacherService.updateByBo(bo));
    }

    /**
     * 删除教师管理
     *
     * @param teacherIds 主键串
     */
    @SaCheckPermission("school:teacher:remove")
    @Log(title = "教师管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{teacherIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] teacherIds) {
        return toAjax(teacherService.deleteWithValidByIds(List.of(teacherIds), true));
    }



    @SaCheckPermission("school:teacher:query")
    @GetMapping(value = "/getteachers/{schoolCampusId}")
    public R<List<TeacherVo>> getSchoolTeachers( @PathVariable Long schoolCampusId) {
        return R.ok(teacherService.findTeachers(schoolCampusId));
    }



    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "教师信息导入模版", TeacherVo.class, response);
    }





    @PostMapping("/upload")
    public R upload(@RequestPart("file") MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), TeacherVo.class, new TeacherImportExcelListener(teacherService,campusService,disciplineService)).sheet().doRead();
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.fail("文件导入失败！");
    }


}
